<html>
<head>
	<meta charset="UTF-8">
	<meta content="IE=edge" http-equiv="X-UA-Compatible">
	<meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width" name="viewport">
	<title>3099：Hash Killer III</title>
	<!-- css -->
	<link href="../css/base.min.css" rel="stylesheet">
	<link href="../css/project.min.css" rel="stylesheet">
	
	<!-- favicon -->
	<!-- ... -->
</head>
<body class="page-brand">
	<header class="header header-transparent header-waterfall ui-header">
		<ul class="nav nav-list pull-left">
			<li>
				<a data-toggle="menu" href="#menu">
					<span class="icon icon-lg">menu</span>
				</a>
			</li>
		</ul>
		<a class="header-logo header-affix-hide margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">Hash Killer III</a>
		<span class="header-logo header-affix margin-left-no margin-right-no" data-offset-top="213" data-spy="affix">Hash Killer III</span>
	</header>
	<nav aria-hidden="true" class="menu" id="menu" tabindex="-1">
		<div class="menu-scroll">
			<div class="menu-content">
				<a class="menu-logo" href="../index.html">BZOJ离线题库</a>
				<ul class="nav">
					<li>
						<a class="waves-attach" data-toggle="collapse" href="#problems">题目</a>
						<ul class="menu-collapse collapse in" id="problems">
							<li>
								<a class="waves-attach" href="../index.html">主页</a>
							</li>
							<li>
								<a class="waves-attach" href="../list.html">题目列表</a>
							</li>
						</ul>
					</li>
					<li>
						<a class="collapsed waves-attach" data-toggle="collapse" href="#about">关于</a>
						<ul class="menu-collapse collapse" id="about">
							<li>
								<a class="waves-attach" href="../about.html">关于此项目</a>
							</li>
						</ul>
					</li>
					
				</ul>
			</div>
		</div>
	</nav>
	<main class="content">
		<div class="content-header ui-content-header">
			<div class="container">
				<h1 class="content-heading">
                Hash Killer III                </h1>
                <p>时间限制：10s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  空间限制：128MB</p>			</div>
		</div>
		<div class="container">
			<section class="content-inner margin-top-no">
				<div class="row">
					<div class="col-lg-13 col-md-13">
						<div class="card margin-bottom-no">
							<div class="card-main">
								<div class="card-inner">
									
                                <h3>题目描述</h3><p><p><span style="font-size: medium">这天天气不错，hzhwcmhf神犇给VFleaKing出了一道题：<br />
给你一个长度为N的字符串S，求有多少个不同的长度为L的子串。<br />
子串的定义是S[l]、S[l + 1]、... S[r]这样连续的一段。<br />
两个字符串被认为是不同的当且仅当某个位置上的字符不同。</span></p>
<p><span style="font-size: medium">VFleaKing一看觉得这不是Hash的裸题么！于是果断写了哈希 + 排序。<br />
而hzhwcmhf神犇心里自然知道，这题就是后缀数组的height中 &lt; L的个数 + 1，就是后缀自动机上代表的长度区间包含L的结点个数，就是后缀树深度为L的结点的数量。<br />
但是hzhwcmhf神犇看了看VFleaKing的做法表示非常汗。于是想卡掉他。</span></p>
<p><span style="font-size: medium">VFleaKing使用的是字典序哈希，其代码大致如下：<br />
u64 val = 0;<br />
for (int i = 0; i &lt; l; i++)<br />
&nbsp;val = (val * base + s[i] - 'a') % Mod;<br />
u64是无符号int64，范围是[0, 2^64)。<br />
base是一个常量，VFleaKing会根据心情决定其值。<br />
Mod是int32范围内的素数。即Mod范围是[0, 2^31)，且是个素数。<br />
VFleaKing还求出来了base ^ l % Mod，即base的l次方除以Mod的余数，这样就能方便地求出所有长度为L的子串的哈希值。<br />
然后VFleaKing给哈希值排序，去重，求出有多少个不同的哈希值，把这个数作为结果。<br />
但是VFleaKing意识到这样会被坑，于是采用了两个Mod来解决问题。两个Mod模出来的值都相同才被认为是相同。<br />
其算法的C++代码如下：</span></p>
<p><span style="font-size: medium">typedef unsigned long long u64;<br />
typedef pair&lt;int, int&gt; PII;</span></p>
<p><span style="font-size: medium">const int MaxN = 100000;</span></p>
<p><span style="font-size: medium">inline int hash_handle(<br />
&nbsp;&nbsp;const char *s, const int &amp;n, const int &amp;l, const int &amp;base,<br />
&nbsp;&nbsp;const int &amp;mod1, const int &amp;mod2)<br />
{<br />
&nbsp;int li_n;<br />
&nbsp;static PII li[MaxN];<br />
&nbsp;u64 hash_pow_l;<br />
&nbsp;u64 val;</span></p>
<p><span style="font-size: medium">&nbsp;hash_pow_l = 1;<br />
&nbsp;for (int i = 1; i &lt;= l; i++)<br />
&nbsp;&nbsp;hash_pow_l = (hash_pow_l * base) % mod1;</span></p>
<p><span style="font-size: medium">&nbsp;li_n = 0;<br />
&nbsp;val = 0;<br />
&nbsp;for (int i = 0; i &lt; l; i++)<br />
&nbsp;&nbsp;val = (val * base + s[i] - 'a') % mod1;<br />
&nbsp;li[li_n++].first = val;<br />
&nbsp;for (int i = l; i &lt; n; i++)<br />
&nbsp;{<br />
&nbsp;&nbsp;val = (val * base + s[i] - 'a') % mod1;<br />
&nbsp;&nbsp;val = (val + mod1 - ((s[i - l] - 'a') * hash_pow_l) % mod1) % mod1;<br />
&nbsp;&nbsp;li[li_n++].first = val;<br />
&nbsp;}</span></p>
<p><span style="font-size: medium">&nbsp;hash_pow_l = 1;<br />
&nbsp;for (int i = 1; i &lt;= l; i++)<br />
&nbsp;&nbsp;hash_pow_l = (hash_pow_l * base) % mod2;</span></p>
<p><span style="font-size: medium">&nbsp;li_n = 0;<br />
&nbsp;val = 0;<br />
&nbsp;for (int i = 0; i &lt; l; i++)<br />
&nbsp;&nbsp;val = (val * base + s[i] - 'a') % mod2;<br />
&nbsp;li[li_n++].second = val;<br />
&nbsp;for (int i = l; i &lt; n; i++)<br />
&nbsp;{<br />
&nbsp;&nbsp;val = (val * base + s[i] - 'a') % mod2;<br />
&nbsp;&nbsp;val = (val + mod2 - ((s[i - l] - 'a') * hash_pow_l) % mod2) % mod2;<br />
&nbsp;&nbsp;li[li_n++].second = val;<br />
&nbsp;}</span></p>
<p><span style="font-size: medium">&nbsp;sort(li, li + li_n);<br />
&nbsp;li_n = unique(li, li + li_n) - li;<br />
&nbsp;return li_n;<br />
}</span></p>
<p><span style="font-size: medium">hzhwcmhf当然知道怎么卡啦！但是他想考考你。<br />
</span></p></p><hr/><h3>输入格式</h3><p><p><font size="4">没有输入。<br />
</font></p></p><hr/><h3>输出格式</h3><p><p><font size="4">你需要输出一组数据使得VFleaKing的代码WA掉。我们会使用Special Judge检查你的结果的正确性。<br />
输出文件共两行。<br />
第一行两个用空格隔开的数n、l。<br />
第二行是一个长度为n的字符串。只能包含'a'~'z'。<br />
需要保证1 &lt;= n &lt;= 10^5, 1 &lt;= l &lt;= n，<br />
不符合以上格式会WA。<br />
不要有多余字符，很可能导致你WA。</font></p>
<p></p></p><hr/><h3>样例输入</h3><pre>没有
</pre><hr/><h3>样例输出</h3><pre>8 4
buaabuaa
（当然这个输出是会WA的）
</pre><hr/><h3>提示</h3><p><p>其实出题人并不知道怎么做&hellip;&hellip;只是把这题挂在这里希望有人能给个解答。<br />
如果这个问题解决了hash应该能算是正式被hack了。</p></p><hr/><h3>题目来源</h3><p>VFleaKing & hzhwcmhf
</p>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				
			</section>
		</div>
	</main>

	<div class="fbtn-container">
		<div class="fbtn-inner">
			<a class="fbtn fbtn-lg fbtn-brand-accent waves-attach waves-circle waves-light waves-effect" data-toggle="dropdown" aria-expanded="true"><span class="fbtn-text fbtn-text-left">Menu</span><span class="fbtn-ori icon">apps</span><span class="fbtn-sub icon">close</span></a>
			<div class="fbtn-dropup">
				<a class="fbtn fbtn-brand waves-attach waves-circle waves-light waves-effect" href="../list.html" target="_self"><span class="fbtn-text fbtn-text-left">题目列表</span><span class="icon">menu</span></a>
				<a class="fbtn fbtn-green waves-attach waves-circle waves-effect" href="../index.html" target="_self"><span class="fbtn-text fbtn-text-left">返回主页</span><span class="icon">home</span></a>
				<a class="fbtn waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/submitpage.php?id=3099" target="_blank"><span class="fbtn-text fbtn-text-left">提交代码</span><span class="icon">send</span></a>
				<a class="fbtn fbtn-orange waves-attach waves-circle waves-effect" href="http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=3099" target="_blank"><span class="fbtn-text fbtn-text-left">试题讨论</span><span class="icon">chat</span></a>
				
			</div>
		</div>
	</div>

	<!-- js -->
	<script src="../js/jquery.min.js"></script>
	<script src="../js/base.min.js"></script>
	<script src="../js/project.min.js"></script>
</body>
</html>